package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/regions")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ServletContext ctx;

    @GetMapping
    @ResponseBody
    public Object list() {

        return  regionService.selectAll();
    }


    //推荐地区
    @GetMapping("/{state}")
    @ResponseBody
    public Object hotList(@PathVariable Integer state) {

        List<Region> regions = regionService.getListByState(state);
        return regions;
    }
    @GetMapping(value = "/{placeId}/strategies",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object searchAll(StrategyQueryObject qo){
        PageInfo list = strategyService.getList(qo);
        return list;
    }

    //查询某个地区下的所有的攻略/推荐攻略接口
    @GetMapping(value = "/{placeId}/strategies",produces = "text/html;charset=utf-8")
    public void hot(StrategyQueryObject qo, HttpServletResponse resp) throws Exception {
        //处理请求,需要返回已经拼接好的所有攻略,要分页
        PageInfo all = strategyService.getList(qo);

        //返回当季攻略,就是推荐的
        qo.setState(Strategy.STATE_COMMEND);
        //而且不分页
        qo.setPageSize(0);
        PageInfo hots = strategyService.getList(qo);
        //----------------------------------
        //创建配置对象,设置版本号就不会过时
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        //指定模板的文件位置
        //要用servletContext 这个容器才有getRealPath
        cfg.setDirectoryForTemplateLoading(new File(ctx.getRealPath("templates")));
        //获取模板,并解决乱码问题
        Template temp = cfg.getTemplate("strategyPage.ftl","UTF-8");
        //合并:数据+模板
        //freemark 只能接受一个数据,使用map打包,因为时pageInfo,所以要变量.list
        Map<String,Object> map = new HashMap<>();
        map.put("alls",all);
        map.put("hots",hots);
        System.out.println(map);
        //输出到页面,类似于modal
        temp.process(map,resp.getWriter());


    }
}
